//
//  SettingController.m
//  GuShiNews
//
//  Created by huachun on 2019/8/16.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "SettingController.h"
#import "MyCenterCell.h"
#import "CenterModel.h"
#import "SettingViewModel.h"
#import "CenterAction.h"
#import "MyLikeController.h"

@interface SettingController ()

@end

@implementation SettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    SettingViewModel *viewmodel = [[SettingViewModel alloc] init:@[]];
    self.Data_array = [NSMutableArray new];
    self.Data_array = [viewmodel.dataSource mutableCopy];
    
    [self setnavback];
    [self.view addSubview:self.TableView];
    self.TableView.separatorInset      = UIEdgeInsetsMake(0, 20, 0, 0);
    self.TableView.separatorStyle      = UITableViewCellSeparatorStyleSingleLineEtched;
    self.TableView.separatorColor      = [JonesClass stringTOColor:@"#e9e9e9"];
    self.TableView.backgroundColor = [JonesClass stringTOColor:@"#fbfbfb"];
    
    self.title = @"设置";
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

#pragma mark - UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.Data_array count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self.Data_array objectAtIndex:section] count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CenterModel *model = [[self.Data_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return model.Height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCenterCell *cell = [MyCenterCell cellForTableView:tableView cellForRowAtIndexPath:indexPath];
    cell.Model = [[self.Data_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            [CenterAction Actiontodone:JonesGOTO_CenterContactUs view:self dictionary:@[]];
        }else if (indexPath.row==1){
            [self clearhuan];
        }else{
            [CenterAction Actiontodone:JonesGOTO_CenterAboutUs view:self dictionary:@[]];
        }
    }else{
        if (indexPath.row==0) {
            MyLikeController *liekvc = [MyLikeController new];
            liekvc.hidesBottomBarWhenPushed = YES;
            liekvc.Here_type = 2;
            [self.navigationController pushViewController:liekvc animated:YES];
        }else{
            [self backuser];
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)backuser{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定要退出吗?" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [UserModel clearUser];
        [[NSNotificationCenter defaultCenter] postNotificationName:ROOTGOTOLOGIN object:@{@"done":@"0"}];
        [self gobackRoot];
    }];
    [alertController addAction:resetAction];
    UIAlertAction *resetActionqu = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertController addAction:resetActionqu];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)clearhuan{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定清除缓存吗" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [SVProgressHUD showWithStatus:@"清理..."];
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            [SVProgressHUD dismiss];
            [self.TableView reloadData];
        }];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:sureAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
